package com.jushu.storbox.util.identity;

import java.io.InputStream;
import java.security.MessageDigest;


public class StreamSha1FileIdentity extends AbsStreamIdentity {

	public StreamSha1FileIdentity(InputStream inputStream) {
		super(inputStream);
	}
	
	public StreamSha1FileIdentity(InputStream inputStream, IidentityCallback callback) {
		super(inputStream, callback);
	}

	@Override
	protected String doComputeIdentity() throws Exception  {
		byte[] buffer = new byte[BUFFER_SIZE];
		int len = 0;
		double currentProgress = 0;
		double progress = 0;
		double oldProgress = 0;
		MessageDigest messagedigest = MessageDigest.getInstance("SHA-1");
		int fileSize = innerStream.available();
		while ((len = innerStream.read(buffer)) > 0) {
			messagedigest.update(buffer, 0, len);
			currentProgress += len;
			oldProgress = currentProgress * 100 / fileSize;
			if ((int) progress != (int) oldProgress) {
				progress = oldProgress;
				IdentityCallback.onProgress((int) progress,
						(long) currentProgress, fileSize);// onProgress
			}
		}
		innerStream.reset();
		identity = byte2hex(messagedigest.digest());
		return identity;
	}

}
